//
//  MeatCartFooter.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/23.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatPayView.h"


@interface MeatPayView()

@end

@implementation MeatPayView

- (void)setupUI{
    
    [self addSubview:self.chooseBtn];
    [self addSubview:self.payBtn];
    [self addSubview:self.payLab];
    [self addSubview:self.deleteBtn];
    [self addSubview:self.clearBtn];
    [self addSubview:self.payDetailLab];
    self.line.hidden = NO;
}

- (void)setupLayout{
    
    self.height = 54.f + TJ_SafeAreaBottomHeight;
    
    //购物车全选
    {
        _payBtn.size = CGSizeMake(125, self.height);
        _payBtn.mj_x = KScreenWidth - _payBtn.width;
        _payBtn.mj_y = 0;

        _chooseBtn.size = CGSizeMake(70, 48);
        _chooseBtn.mj_x = 20;
        _chooseBtn.mj_y = (self.height - self.chooseBtn.height)/2.0;
        
        _payLab.mj_x = _chooseBtn.right + 10;
        _payLab.mj_y = 0;
        _payLab.height = self.height;
        _payLab.width = KScreenWidth - _chooseBtn.right - _payBtn.width- 20;
    }
    
    //购物车编辑
    {
        _deleteBtn.size = CGSizeMake(82, 30);
        _deleteBtn.mj_x = KScreenWidth - _deleteBtn.width - 15;
        _deleteBtn.mj_y = (self.height - self.deleteBtn.height)/2.0;
        
        _clearBtn.size = CGSizeMake(82, 30);
        _clearBtn.mj_x = _deleteBtn.left - _clearBtn.width - 12;
        _clearBtn.mj_y = (self.height - self.clearBtn.height)/2.0;
    }
    
    //购物车订单提交
    {
        _payDetailLab.size = CGSizeMake(150, 17);
    }
}


#pragma mark - setter methods

- (void)setType:(MeatPayType)type{
    _type = type;
    
    switch (type) {
        case MeatPayType_WholeChoose:
        {
            @weakify(self);
            [UIView animateWithDuration:AnimationTime animations:^{
                @strongify(self);
                self.chooseBtn.alpha = self.payBtn.alpha =  self.payLab.alpha =  1;
                self.clearBtn.alpha = self.deleteBtn.alpha =  0;
            }];
        }
            break;
            
        case MeatPayType_Edit:
        {
            
            @weakify(self);
            [UIView animateWithDuration:AnimationTime animations:^{
                @strongify(self);
                self.chooseBtn.alpha = self.payBtn.alpha =  self.payLab.alpha =  0;
                self.clearBtn.alpha = self.deleteBtn.alpha =  1;
                [self.clearBtn setTitle:@"清空失效" forState:UIControlStateNormal];
                [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
            }];
            
        }
            break;
        
        case MeatPayType_NoTip:
        {
            _payLab.mj_x = 15;
            _payLab.mj_y = 0;
            _payLab.width = KScreenWidth - _payBtn.width - 20;
            _payLab.height = self.height;
            _payLab.textAlignment = NSTextAlignmentLeft;
            
            _payDetailLab.alpha = 0;
            _chooseBtn.alpha = 0;
        }
            break;
            
        case MeatPayType_SeparatePlan:
        {
           self.payBtn.alpha =  self.payLab.alpha =  0;
           self.chooseBtn.alpha = self.clearBtn.alpha = self.deleteBtn.alpha = 1;
           [self.clearBtn setTitle:@"预留" forState:UIControlStateNormal];
            [self.deleteBtn setTitle:@"销售" forState:UIControlStateNormal];

        }
            break;
            
        case MeatPayType_SeparateCopy:
        {
            self.payBtn.alpha =  self.payLab.alpha =  0;
            self.chooseBtn.alpha = self.clearBtn.alpha = self.deleteBtn.alpha = 1;
            [self.clearBtn setTitle:@"一键赠送" forState:UIControlStateNormal];
            [self.deleteBtn setTitle:@"一键发单" forState:UIControlStateNormal];
            
        }
            break;

        case MeatPayType_OrderDetail:
        {
            self.payBtn.alpha =  self.payLab.alpha = self.chooseBtn.alpha = self.clearBtn.alpha= 0;
            self.deleteBtn.alpha = 1;
            [self.deleteBtn setTitle:@"冷链物流" forState:UIControlStateNormal];
            
        }
            break;

            
        default:
        {
            _payLab.mj_x = 15;
            _payLab.width = KScreenWidth - _payBtn.width - 20;
            _payLab.height = 30 + TJ_SafeAreaBottomHeight/2.0;
            _payLab.textAlignment = NSTextAlignmentLeft;

            _payDetailLab.alpha = 1;
            _payDetailLab.mj_x = 8;
            _payDetailLab.mj_y = _payLab.bottom + 1;
            
            _chooseBtn.alpha = 0;

        }
            break;
    }
}

- (void)setPrice:(NSString *)price{
    [_payLab setAttributedText:[self getPayText:price]];
}


#pragma mark -  getter methods

- (UIButton *)chooseBtn{
    if (!_chooseBtn) {
        _chooseBtn = [UIButton makeButtonWithText:@"全选" textColor:UIColorFromRGB(0x666666) font:mFont(14) textAliment:1 cornerRadius:0];
        [_chooseBtn setImage:[UIImage imageNamed:@"meat_cart_unchecked"] forState:UIControlStateNormal];
        [_chooseBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        _chooseBtn.isNeedAnimation = YES;
        _chooseBtn.selected = YES;
        
        kWeakSelf(self);
        _chooseBtn.tapBlock = ^{
            
            UIImage *image = nil;
            if (weakself.chooseBtn.selected) {
                weakself.chooseBtn.selected = NO;
                image = [UIImage imageNamed:@"adopt_buy_selected"];
            }else{
                weakself.chooseBtn.selected = YES;
                image = [UIImage imageNamed:@"meat_cart_unchecked"];
            }
            
            [weakself.chooseBtn setImage:image forState:UIControlStateNormal];
            
            if (weakself.chooseCartBlock) {
                weakself.chooseCartBlock(weakself.chooseBtn);
            }
        };
    }
    return _chooseBtn;
}


- (UIButton *)payBtn{
    if (!_payBtn) {
        _payBtn = [UIButton makeButtonWithText:@"去结算" textColor:KWhiteColor font:mFont(15) textAliment:1 cornerRadius:0];
        [_payBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_pay_right"] forState:UIControlStateNormal];
        _payBtn.isNeedAnimation = YES;
        
        kWeakSelf(self);
        _payBtn.tapBlock = ^{
            if (weakself.payCartBlock) {
                weakself.payCartBlock();
            }
        };
    }
    return _payBtn;
}


- (UIButton *)deleteBtn{
    
    if (!_deleteBtn) {
        _deleteBtn = [UIButton makeButtonWithText:@"删除" textColor:KWhiteColor font:mFont(13) textAliment:1 cornerRadius:15];
        [_deleteBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_pay_right"] forState:UIControlStateNormal];
        _deleteBtn.layer.borderColor = UIColorFromRGB(0xD8D8D8).CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.alpha = 0;
        _deleteBtn.isNeedAnimation = YES;
        
        kWeakSelf(self);
        _deleteBtn.tapBlock = ^{
            if (weakself.deleteCartBlock) {
                weakself.deleteCartBlock();
            }
        };
     }
    return _deleteBtn;
}


- (UIButton *)clearBtn{
    
    if (!_clearBtn) {
        _clearBtn = [UIButton makeButtonWithText:@"清空失效" textColor:UIColorFromRGB(0x454545) font:mFont(13) textAliment:1 cornerRadius:15];
        _clearBtn.layer.borderColor = UIColorFromRGB(0xD8D8D8).CGColor;
        _clearBtn.layer.borderWidth = 1;
        _clearBtn.alpha = 0;
        _clearBtn.isNeedAnimation = YES;
        
        kWeakSelf(self);
        _clearBtn.tapBlock = ^{
            if (weakself.clearCartBlock) {
                weakself.clearCartBlock();
            }
        };
    }
    return _clearBtn;
}


- (UILabel *)payLab{
    if (!_payLab) {
        _payLab = [[UILabel alloc] init];
        _payLab.textAlignment = NSTextAlignmentCenter;
    }
    return _payLab;
}


- (UILabel *)payDetailLab{
    if (!_payDetailLab) {
        _payDetailLab = [UILabel makeLabelWithText:@"(不含预定单费用）" textColor:UIColorFromRGB(0x999999) font:mFont(12) textAliment:0];
        _payDetailLab.alpha = 0;
    }
    return _payDetailLab;
}



#pragma mark - other methods

- (NSMutableAttributedString *)getPayText:(NSString *)text{
    
    NSString *payTipS = @"合计 ：";
    NSString *moneySymbol = @"¥";
    NSString *totalS = [NSString stringWithFormat:@"%@%@%@", payTipS, moneySymbol, text];
    
    NSMutableAttributedString *attributedS = [[NSMutableAttributedString alloc] initWithString:totalS];
    
    [attributedS addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333) range:NSMakeRange(0, 4)];
    [attributedS addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xF25C62) range:NSMakeRange(4, totalS.length -4)];
    [attributedS addAttribute:NSFontAttributeName value:mFont(13) range:NSMakeRange(0, 4)];
    [attributedS addAttribute:NSFontAttributeName value:mFont(12) range:NSMakeRange(4, 1)];
    [attributedS addAttribute:NSFontAttributeName value:mMediumFont(17) range:NSMakeRange(5, totalS.length - 5)];
    
    return attributedS;
}

@end
